from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse
from django.urls import reverse
import json


# from flask import request

# Create your views here.


# @app.route()
def index(request):
    print(request.method)

    # 完成业务逻辑
    # raise Exception('1111111')

    url = reverse('user:index')
    print(url)  # user/index

    return HttpResponse('hello ming')


def say(request):
    return HttpResponse('say')


def sayhello(request):
    return HttpResponse('sayhello')


# url路径参数匹配
def weather(request, city, year):
    print(year)
    print(city)
    return HttpResponse('weather')


def request_func(request):
    # 获取查询字符串数据
    # data=request.GET.get('a')
    # data_list=request.GET.getlist('a')
    # print(data)
    # print(data_list[0])

    # 获取请求体数据   表单数据
    # data=request.POST.get('a')
    # data_list=request.POST.getlist('a')
    # print(data)
    # print(data_list)
    # 获取请求体数据   非表单数据
    data = request.body  # 获取的byte类型
    data = data.decode()
    # print(type(data))
    data_dict = json.loads(data)
    # print(data_dict['itcast'])

    # 获取头部数据
    data = request.META['CONTENT_TYPE']
    # print(data)

    # print(request.method)
    # print(request.path)
    # print(request.user)
    print(request.FILES)

    return HttpResponse('request_func')


def response_func(request):
    # 响应对象
    response = HttpResponse('response_func', status=404)
    # # response.status_code=400
    # response['itcast']='python'

    # 子类
    # return HttpResponseNotFound('response_func')


    data = {'itcast': 'python11111'}
    # data=json.dumps(data)

    # 返回json数据
    # return JsonResponse(data)

    url = reverse('user:index')
    # 跳转
    return redirect(url)


def cookie_func(request):
    # 创建响应对象
    response = HttpResponse('cookie_func')
    # 写入cookie
    response.set_cookie('itcast', 'python', max_age=1000)

    # 获取cookie
    data = request.COOKIES.get('itcast')
    print(data)

    return response


def session_func(request):
    # 写入session
    request.session['itcast'] = 'python'
    request.session['name'] = 'ming'

    # 获取session
    # data=request.session.get('itcast')
    # print(data)

    # 清除session
    # request.session.clear()
    # request.session.flush()

    # 删除数据
    del request.session['itcast']



    return HttpResponse('session_func')
